package com.bruan.controller;

import com.bruan.anno.MyLog;
import com.bruan.pojo.Result;
import com.bruan.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传{}{}{}",username,age,image);
//        //构造唯一的文件名 - uuid (通用唯一识别码)
//        String originalFilename = image.getOriginalFilename();
//        String newFilename = UUID.randomUUID().toString();
//        //文件扩展名
//        int lastIndex = originalFilename.lastIndexOf(".");
//        String fileExtName = originalFilename.substring(lastIndex);
//        log.info("文件名称：{}{}",newFilename,fileExtName);
//        //将文件上传到 本地
//        image.transferTo(new File("D:\\Desktop\\idea_file_temp\\"+newFilename+fileExtName));
//        return Result.success();
//    }


    @MyLog
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        String originName = image.getOriginalFilename();
        log.info("文件上传{}",originName);
        //调用oss阿里云进行上传
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成，文件访问的url：{}",url);
        return Result.success(url);
    }

}
